package com.springbootjsp.controller;

import com.alibaba.fastjson.JSONObject;
import com.springbootjsp.model.User;
import com.springbootjsp.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: szw
 * @Date: 2021/6/10 16:37
 */
@RestController
public class LoginController {

    @Resource
    TestService testService;


    @RequestMapping("/login")
    public JSONObject login(String name, String password) {
        System.out.println("name="+ name + ";password=" + password);

        // 模拟操作
        JSONObject json = new JSONObject();
        json.put("code", 9999);
        json.put("msg", "登录失败，密码错误！");
        if ("张三".equals(name) && "123456".equals(password)) {
            json.put("code", 200);
            json.put("msg", "登录成功");
            json.put("data", "返回数据");
        }

        return json;
    }


    @RequestMapping("/login1")
    public JSONObject login1(String name,String pwd){
        System.out.println("name"+name+":pwd"+pwd);
        User user = new User(name,pwd);
        List<User> login = testService.login(user);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",9999);
        jsonObject.put("msg","登陆失败");
        if(login!=null){
            jsonObject.put("code",200);
            jsonObject.put("msg","登陆成功");
            jsonObject.put("data",login);
        }
        return jsonObject;
    }

    @RequestMapping("/insert")
    public JSONObject insert(User user){
        System.out.println("用户信息"+user);
        boolean b = testService.insertUser(user);
        System.out.println(b);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",9999);
        jsonObject.put("msg","注册失败");
        if(b){

            jsonObject.put("code",200);
            jsonObject.put("msg","注册成功");
            jsonObject.put("data",user);
        }
        return jsonObject;
    }

}
